package cn.edu.jxust.core.infrastructure.mapper;

import cn.edu.jxust.common.domain.entity.Device;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;

/**
 * @author QiuKangming
 * @since 2020-06-29
 */

public interface DeviceMapper extends BaseMapper<Device> {

    /**
     * 根据IMEI获取设备的id
     * @param imei 设备唯一标识号
     * @return Integer
     */
    @Select("SELECT id FROM t_device WHERE nb_device_id = #{imei} AND status = 1")
    Integer getIdByIMEI(String imei);

    /**
     * 获取设备绑定的监测点id号
     * @param id 设备id
     * @return Integer
     */
    @Select("SELECT device_site_id FROM t_device WHERE is_binding = 1 AND id = #{id}")
    Integer getSiteIdById(Integer id);


    /**
     * 根据监测点id号获取设备id号
     * @param siteId 监测点id号
     * @return Integer
     */
    @Select("SELECT id FROM t_device WHERE device_site_id = #{siteId} AND is_binding = 1 AND status = 1")
    Integer getIdBySiteId(Integer siteId);

    /**
     * 根据区域id获取该区域的设备ids
     * @param areaId 区域id
     * @return List<Integer>
     */
    @Select("SELECT id FROM t_device WHERE area_id = #{areaId} AND status = 1")
    List<Integer> getDeviceIdsByAreaId(Integer areaId);

    /**
     * 根据区域id和企业id获取该企业的设备id列表
     * @param areaId 区域id
     * @param enterpriseId 企业id
     * @return List<Integer>
     */
    @Select("SELECT id FROM t_device WHERE area_id = #{areaId} AND enterprise_id = #{enterpriseId} AND status = 1")
    List<Integer> getDeviceIdsByAreaIdEnterpriseId(Integer areaId, Integer enterpriseId);

    /**
     * 根据设备id号查询设备唯一标识码
     * @param deviceId 设备id
     * @return String
     */
    @Select("SELECT nb_device_id FROM t_device WHERE id = #{deviceId} AND status = 1")
    String getIMEIById(Integer deviceId);

    /**
     * 根据 区域id和企业id查询该企业下的设备个数
     * @param areaId 区域id
     * @param enterpriseId 企业id
     * @return int
     */
    @Select("SELECT COUNT(1) FROM t_device WHERE area_id = #{areaId} AND enterprise_id = #{enterpriseId} AND status = 1")
    int checkDeviceCountByAreaIdAndEnterpriseId(Integer areaId, Integer enterpriseId);

    /**
     * 根据deviceId获取设备是否被绑定
     * @param deviceId 设备id号
     * @return int, 1 绑定, 0 未绑定
     */
    @Select("SELECT is_binding FROM t_device WHERE id = #{deviceId} AND status = 1")
    int deviceIsBinding(Integer deviceId);
}
